package cc.mrbird.grid.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.common.domain.Tree;
import cc.mrbird.common.util.FileUtil;
import cc.mrbird.grid.domain.GridInfo;
import cc.mrbird.grid.service.GridInfoService;
import cc.mrbird.management.domain.ManagementInstitutions;
import cc.mrbird.management.service.ManagementInstitutionsService;
import cc.mrbird.system.domain.Menu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(description = "管理机构接口")
@RestController
public class GridInfoController extends BaseController{
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private GridInfoService gridInfoService;

    @Log("获取网格信息")
    @RequestMapping(value = "gridInfo",method = RequestMethod.POST)
   /* @RequiresPermissions("management:list")*/
    public String index() {
        return "grid/gridInfo/gridInfo";
    }
    @Log("获取网格信息")
    @ApiOperation(value = "获取网格信息" ,  notes="获取网格信息")
    @RequestMapping(value = "gridInfo/list",method = RequestMethod.POST)
    /*@RequiresPermissions("gridInfo:list")*/
    @ResponseBody
    public Map<String, Object> gridInfoList(QueryRequest request, GridInfo gridInfo) {
        return super.selectByPageNumSize(request, () -> this.gridInfoService.findGridInfoList(gridInfo, request));
    }




    @RequestMapping("gridInfo/tree")
    @ResponseBody
    public ResponseBo getMenuTree() {
        try {
            Tree<GridInfo> tree = this.gridInfoService.getGridTree();
            return ResponseBo.ok(tree);
        } catch (Exception e) {
            log.error("获取菜单树失败", e);
            return ResponseBo.error("获取菜单树失败！");
        }
    }

    @RequestMapping(value = "gridInfoParent/list",method = RequestMethod.POST)
    /*@RequiresPermissions("gridInfoParent:list")*/
    @ResponseBody
    public List<GridInfo> findAllGrids() {
        try {
            return this.gridInfoService.findAllGrids();
        } catch (Exception e) {
            log.error("获取父网格集合失败", e);
            return new ArrayList<>();
        }
    }
    @ApiOperation(value = "导出Excel")
    @RequestMapping(value = "gridInfo/excel",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo managementExcel(GridInfo gridInfo){
        try {
            List<GridInfo> list = this.gridInfoService.findGridInfoList(gridInfo, null);
            return FileUtil.createExcelByPOIKit("网格信息表", list, GridInfo.class);
        } catch (Exception e) {
            log.error("导出网格信息表Excel失败", e);
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }

    @ApiOperation(value = "导出csv")
    @RequestMapping(value = "gridInfo/csv",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo managementCsv(GridInfo gridInfo) {
        try {
            List<GridInfo> list = this.gridInfoService.findGridInfoList(gridInfo, null);
            return FileUtil.createCsv("网格信息", list, GridInfo.class);
        } catch (Exception e) {
            log.error("导出管理机构表Csv失败", e);
            return ResponseBo.error("导出Csv失败，请联系网站管理员！");
        }
    }
    @ApiOperation(value = "导入excel")
    @RequestMapping(value = "gridInfo/import",method = RequestMethod.POST)
    public ResponseBo eximport(@RequestParam("file") MultipartFile file) {
        boolean a = false;
        String fileName = file.getOriginalFilename();
        try {
            a = gridInfoService.batchImport(fileName, file);
            return  ResponseBo.ok(a);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  ResponseBo.error("导入数据失败,请重新检查excel数据导入");
    }

    @RequestMapping(value = "gridInfo/getMana" , method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo getMana(Long id) {
        try {
            GridInfo gridInfo = this.gridInfoService.findById(id);
            return ResponseBo.ok(gridInfo);
        } catch (Exception e) {
            log.error("获取网格信息失败", e);
            return ResponseBo.error("获取网格信息失败，请联系网站管理员！");
        }
    }

    @ApiOperation(value = "网格信息添加")
    @RequestMapping(value = "gridInfo/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo addMana(GridInfo gridInfo) {
        try {
            GridInfo result = this.gridInfoService.findByName(gridInfo.getGrid_name());
            if (result != null) {
                return ResponseBo.warn("该用户名已被使用！");
            }
            this.gridInfoService.add(gridInfo);
            return ResponseBo.ok();
        } catch (Exception e) {
            log.error("添加失败", e);
            return ResponseBo.error("添加失败，请重新添加！");
        }
    }

    @ApiOperation(value = "修改网格信息")
    @Log("修改网格信息")
   /* @RequiresPermissions("gridInfo:update")*/
    @RequestMapping(value = "gridInfo/update" ,method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo updateMana(GridInfo gridInfo) {
        try {
            this.gridInfoService.updateMana(gridInfo);
            return ResponseBo.ok("修改网格信息成功！");
        } catch (Exception e) {
            log.error("修改网格信息失败", e);
            return ResponseBo.error("修改网格信息失败，请联系网站管理员！");
        }
    }

    @Log("删除网格信息")
    /*@RequiresPermissions("gridInfo:delete")*/
    @RequestMapping(value = "gridInfo/delete",method = RequestMethod.GET)
    @ResponseBody
    public ResponseBo deleteManagement(String ids) {
        try {
            this.gridInfoService.deleteMana(ids);
            return ResponseBo.ok("删除网格信息成功！");
        } catch (Exception e) {
            log.error("删除网格信息失败", e);
            return ResponseBo.error("删除网格信息失败，请联系网站管理员！");
        }
    }



}
